<?

if (!file_exists("config.php") && !file_exists("limba/en.php") && !file_exists("configuration.php")) {
	die('Missing files');
} else {
	include_once "config.php";
	include_once "limba/en.php";
	include_once "configuration.php";
}

$rezultat = mysql_query("select * from " . $db["config"] . " where optiune='a_site'");
$rand = mysql_fetch_array($rezultat);
if (count($rand) > 0)
	if ($rand["value"] == '2')
		die("<h2>".$lang["site_inactive"]."</h2>");

function v_mail($str) {
	/*
	 * valid mail
	 * return 1 daca mail-ul este bun, altfel returneaza 0
	 */
	if (strlen($str) > 4)
		return 1;
	return 0;
}

function users_connected() {
	global $db;
	$rezultat = mysql_query("select * from " . $db["users"] . " where logat='1'");
	return mysql_num_rows($rezultat);
}

function juc_clasament($pagina) {
	global $db;
	$pagina*=10;
	$rezultat = mysql_query("select * from " . $db["users"] . " order by puncte desc limit " . $pagina . ",20");
	if (mysql_num_rows($rezultat) == 0)
		return false;
	else {
		$jucatori = array();
		$k = 0;
		while ($rand = mysql_fetch_array($rezultat)) {
			$jucatori[$k] = $rand;
			$k++;
		}
		return $jucatori;
	}
}

function config() {
	global $db;
	$rezultat = mysql_query("select * from " . $db["config"] . "");
	if (mysql_num_rows($rezultat) > 0) {
		$configs = array();
		while ($rand = mysql_fetch_array($rezultat)) {
			$configs[$rand["optiune"]] = $rand["value"];
		}
		return $configs;
	}
	else
		return false;
}

function out_room($id) { //functie folosita pentru a iesi din batalia actuala
	global $db;
	$room = room($id);
	if ($room !== false) {
		$rezultat = mysql_query("select * from " . $db["messages"] . " where room='" . $room["id"] . "' and user='" . $room["user"] . "'");
		$rezultat2 = mysql_query("select * from " . $db["messages"] . " where room='" . $room["id"] . "' and user='" . $room["user2"] . "'");
		if (mysql_num_rows($rezultat) > 0 && mysql_num_rows($rezultat2) > 0) {
			$config = config();
			if ($config !== false)
				$time = a_ceas($config["time_battle2"]);
			else
				$time = a_ceas($terminare_b);
			mysql_query("update " . $db["rooms"] . " set terminat=1, end='" . $time . "' where id='" . $room["id"] . "'");
		}
		else
			delete_room($room["id"]);
	}
}

function ver_rooms2() {
	global $db;
	$time = ceas();
	$rezultat = mysql_query("select * from " . $db["rooms"] . " where terminat='1' and end<'" . $time . "'");
	if (mysql_num_rows($rezultat) > 0) {
		while ($rand = mysql_fetch_array($rezultat)) {
			if ($rand["votes"] > $rand["votes2"]) {
				$user2 = user($rand["user"]);
				if ($user2 !== false) {
					$user2["puncte"]+=1;
					mysql_query("update " . $db["users"] . " set puncte='" . $user2["puncte"] . "' where id='" . $user2["id"] . "'");
				}
			} elseif ($rand["votes"] < $rand["votes2"]) {
				$user2 = user($rand["user2"]);
				if ($user2 !== false) {
					$user2["puncte"]+=1;
					mysql_query("update " . $db["users"] . " set puncte='" . $user2["puncte"] . "' where id='" . $user2["id"] . "'");
				}
			}
			delete_room($rand["id"]);
		}
	}
}

function delete_room($room) {
	global $db;
	mysql_query("delete from " . $db["rooms"] . " where id='" . $room . "'");
	mysql_query("delete from " . $db["rooms"] . " where room='" . $room . "'");
	mysql_query("delete from " . $db["messages"] . " where room='" . $room . "'");
}

function ok_vote($room, $user) {
	global $db;
	$rezultat = mysql_query("select * from " . $db["votes"] . " where room='" . $room . "' and user='" . $user . "'");
	if (mysql_num_rows($rezultat) == 0)
		return 1;
	else
		return 0;
}

function conv_user($room, $user) {
	global $db;
	$rezultat = mysql_query("select * from " . $db["messages"] . " where user='" . $user . "' and room='" . $room . "'");
	if (mysql_num_rows($rezultat) > 0) {
		$conv = array();
		$k = 0;
		while ($rand = mysql_fetch_array(($rezultat))) {
			$conv[$k] = $rand["content"];
			$k++;
		}
		return $conv;
	}
	else
		return false;
}

function mess($id) {
	global $db;
	$room = room($id);
	if ($room["user"] == $_SESSION["user"])
		$class = "stanga";
	else
		$class = "dreapta";
	$continut = '<div class="replica_' . $class . '">' . $room["message"] . '</div><br/>';
	$rezultat = mysql_query("select * from " . $db["messages"] . " where room='" . $room["id"] . "' order by id asc");
	if (mysql_num_rows($rezultat) > 0) {
		while ($rand = mysql_fetch_array($rezultat)) {
			if ($rand["user"] == $_SESSION["user"])
				$class = "stanga";
			else
				$class = "dreapta";
			$continut.='<div class="replica_' . $class . '">' . $rand["content"] . '</div><br/>';
		}
	}
	return $continut;
}

function mess2($id) {
	global $db;
	$room = room($id);
	$class = "stanga";
	$continut = '<div class="replica_' . $class . '">' . $room["message"] . '</div><br/>';
	$rezultat = mysql_query("select * from " . $db["messages"] . " where room='" . $room["id"] . "' order by id asc");
	if (mysql_num_rows($rezultat) > 0) {
		while ($rand = mysql_fetch_array($rezultat)) {
			if ($rand["user"] == $room["user"])
				$class = "stanga";
			else
				$class = "dreapta";
			$continut.='<div class="replica_' . $class . '">' . $rand["content"] . '</div><br/>';
		}
	}
	return $continut;
}

function rooms_d() { //camere care s-au terminat
	global $db;
	$rezultat = mysql_query("select * from " . $db["rooms"] . " where terminat='1' order by end desc");
	if (mysql_num_rows($rezultat) == 0)
		return false;
	else {
		$rooms = array();
		$k = -1;
		while ($rand = mysql_fetch_array($rezultat)) {
			$k++;
			$rooms[$k] = $rand;
		}
		return $rooms;
	}
}

function ver_rooms() { //sterg toate camerele care au depasit timpul de terminare
	global $db;
	$ceas = ceas();
	mysql_query("delete from " . $db["rooms"] . " where terminat='0' and start='0000-00-00 00:00:00' and end<'" . $ceas . "'");
	mysql_query("update " . $db["rooms"] . " set terminat='1', end='" . $ceas . "' where end<'" . $ceas . "' and terminat!='1'");
}

function location($locatie) { //m-am gandit ca ar fi mai rapid sa scriu logatie(battles) decat header("Location: logged.php?display=battles")
	header("Location: logged.php?display=" . $locatie);
	die();
}

function msg($str) { //afiseaza un mesaj, de exemplu in caz ca nu poti crea o camera, folosesti msg("nu poti crea");
	header("Location: logged.php?display=message&message=" . clean($str));
}

function ok_create($id) { //returneaza 1 daca jucatorul are voie sa creeze o camera, altfel returneaza 0
	global $nr_min, $db;
	$user = user($id);
	if ($user === false)
		return 0;
	if ($user["rooms"] < $nr_min)
		return 0;
	$rezultat = mysql_query("select * from " . $db["rooms"] . " where user='" . $id . "' and terminat='0'");
	if (mysql_num_rows($rezultat) > 0) //un jucator poate crea doar o camera
		return 0;
	return 1;
}

function room($id) {
	global $db;
	$rezultat = mysql_query("select * from " . $db["rooms"] . " where id='" . $id . "'");
	$rand = mysql_fetch_array($rezultat);
	if (count($rand) > 1)
		return $rand;
	else
		return false;
}

function rooms_u($user) { //camerele active ale unui jucator
	global $db;
	$rezultat = mysql_query("select * from " . $db["rooms"] . " where (user='" . $user . "' or user2='" . $user . "') and terminat=0");
	if (mysql_num_rows($rezultat) == 0)
		return false;
	else {
		$rooms = array();
		$k = -1;
		while ($rand = mysql_fetch_array($rezultat)) {
			$k++;
			$rooms[$k] = $rand;
		}
		return $rooms;
	}
}

function rooms($user) {
	global $db;
	$rezultat = mysql_query("select * from " . $db["rooms"] . " where terminat=0 and (user!='" . $user . "' and user2!='" . $user . "') order by end desc");
	if (mysql_num_rows($rezultat) > 0) {
		$rooms = array();
		for ($k = 0; $rand = mysql_fetch_array($rezultat); $k++)
			$rooms[$k] = $rand;
		return $rooms;
	}
	else
		return false;
}

function clasament($rang) {
	global $db;
	if ($rang % 20 != 0)
		$rang = (int) ($rang / 20) + 1;
	else
		$rang = (int) ($rang / 20);
	$rezultat = mysql_query("select * from " . $db["users"] . " order by rang desc limit " . ($rang * 20) . ",20");
	if (mysql_num_rows($rezultat) > 0) {
		$jucatori = array();
		for ($k = 0; $rand = mysql_fetch_array($rezultat); $k++)
			$jucatori[$k] = $rand;
		return $jucatori;
	}
	else
		return false;
}

function update_clasament() {
	global $db;
	$rezultat = mysql_query("select * from " . $db["users"] . " order by puncte desc");
	if (mysql_num_rows($rezultat) > 0) {
		$k = 0;
		while ($rand = mysql_fetch_array($rezultat)) {
			$k++;
			mysql_query("update " . $db["users"] . " set rang='" . $k . "' where id='" . $rand["id"] . "'");
		}
	}
}

function a_ceas($secunde) { //imi returneaza ora actuala+secundele care i le dau eu
	$data = strftime("%Y-%m-%d %H:%M:%S", strtotime(ceas()) + $secunde);
	return $data;
}

function ceas() {
	$query = "SELECT timediff(now(), '" . date("Y-m-d H:i:s") . "')";
	$result = mysql_query($query);
	$tdif = mysql_fetch_array($result);
	$tdif = explode(":", $tdif[0]);
	if ($tdif[0][0] == "-") {
		$tdif[0] = abs($tdif[0]);
		$tdif[3] = -1;
	} else
		$tdif[3] = 1;
	$secunde = $tdif[0] * 3600 + $tdif[1] * 60 + $tdif[2];
	$secunde*=$tdif[3];
	$data = strftime("%Y-%m-%d %H:%M:%S", strtotime(date("Y-m-d H:i:s")) + $secunde);
	return $data;
}

function user($id) {
	global $db;
	$rezultat = mysql_query("select * from " . $db["users"] . " where id='" . $id . "'");
	$rand = mysql_fetch_array($rezultat);
	if (count($rand) > 1)
		return $rand;
	else
		return false;
}

function message_i($mesaj) {
	$_SESSION["mesaj"] = clean($mesaj);
	header("Location: index.php");
	die();
}

function message_r($mesaj) {
	$_SESSION["mesaj"] = clean($mesaj);
	header("Location: register_i.php");
	die();
}

function clean(&$str) {
	if (is_numeric($str))
		$str = floor($str);
	$cleaned = strip_tags($str);
	$cleaned = htmlspecialchars($cleaned);
	$cleaned = mysql_real_escape_string($cleaned);
	$to_clean = array("%20", "\"", "'", "\\", ";");
	$cleaned = str_replace($to_clean, "", $cleaned);
	$str = $cleaned;
	return $cleaned;
}

function back() {
	echo "<script type='text/javascript'>history.go(-1)</script>";
}

function iesire() {
	global $db;
	if (isset($_SESSION["admin"]))
		mysql_query("update " . $db["users"] . " set logat='0' where id='" . $_SESSION["admin"] . "'");
	session_destroy();
	header('Location: index.php');
	die();
}

?>
